/**
 * Created by lintao_alex on 2018/1/6.
 */


namespace game{
    import SpriteSheet = egret.SpriteSheet;

    export class GameGLoader extends fairygui.GLoader{
        protected loadExternal():void{
            if(this.url.indexOf('/')<0 && this.url.indexOf('.')>0){
                RES.getResAsync(this.getStringPrefix(this.url), this.gotSheet, this);
            }else if(RES.hasRes(this.url)){
                RES.getResAsync(this.url, this.gotRes, this);
            }else{
                super.loadExternal();
            }
        }

        private gotRes(res:any){
            if(res instanceof egret.Texture){
                this.onExternalLoadSuccess(res)
            }else{
                this.onExternalLoadFailed();
            }
        }

        private gotSheet(sheet:SpriteSheet){
            if(!sheet){
                this.onExternalLoadFailed()
            }else{
                let res = sheet.getTexture(this.getStringTail(this.url));
                if(res){
                    this.onExternalLoadSuccess(res);
                }else{
                    this.onExternalLoadFailed();
                }
            }
        }

        private getStringTail(name:string):string{
            if(!name){
                return "";
            }
            let index:number = name.indexOf(".");
            if(index!=-1) {
                return name.substring(index+1);
            }
            return "";
        }
        private getStringPrefix(name:string):string{
            if(!name){
                return "";
            }
            let index:number = name.indexOf(".");
            if(index!=-1) {
                return name.substring(0, index);
            }
            return "";
        }

    }
}